package app;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;



public class CurrencyConverterController  {

	CurrencyConverterView view;
	CurrencyConverterModel model;
	State state;

	public CurrencyConverterController(CurrencyConverterView view, CurrencyConverterModel model) {
		super();
		this.model = model;
		this.view = view;
		view.setQuitActionListner(new QuitlActionListener());
		view.setComputeActionListner(new ComputeActionListener());
		view.setClearActionListner(new ClearActionListener());
		view.setUSDChangedActionListner(new USAmtChangeListener());
		view.setBrazilSelectedActionListner(new BrazilSelectActionListener());
		view.setCanadaSelectedActionListner(new CanadaSelectActionListener());
		view.setEUSelectedActionListner(new EUSelectActionListener());
		view.setJapanSelectedActionListner(new JapanSelectActionListener());
		
	}

	private class QuitlActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {
			System.exit(0);
		}
	}
	
	private class ComputeActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {
			try{
			model.compute();
			if(model.getState() instanceof State_EquivAmtDisplayed)
				view.displayResult();
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(null, ex.getMessage());
			}
		}
	}
	private class ClearActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {	
			model.clear();
			view.clear();
		}
	}
	
	private class BrazilSelectActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {	
			model.countrySelected(new Brazil());
		}
	}
	private class CanadaSelectActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {	
			
			model.countrySelected(new Canada());
		}
	}
	private class EUSelectActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {	
			
			model.countrySelected(new Europe());
		}
	}
	private class JapanSelectActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent ae) {	
			
			model.countrySelected(new Japan());
		}
	}
	
	private class USAmtChangeListener implements DocumentListener {
		
		@Override
		public void removeUpdate(DocumentEvent e) {
			model.usDollarAmtModified(view.txtFldUSDollarAmount.getText());
		}
		
		@Override
		public void insertUpdate(DocumentEvent e) {
			model.usDollarAmtModified(view.txtFldUSDollarAmount.getText());
		}
		
		@Override
		public void changedUpdate(DocumentEvent e) {
			
		}
	}
}
